package org.example.groupwarehouse.service.admin;

import jakarta.annotation.PostConstruct;
import org.example.groupwarehouse.entity.Admin;
import org.example.groupwarehouse.entity.Users;
import org.example.groupwarehouse.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service("adminserviceimpl")
public class AdminServiceimpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String account, String password) {
        Admin admin = adminMapper.getAdminByAccount(account);
        //判断密码是否一致
        //MD5加密
        if(admin != null && admin.getStatus() == 1){
            boolean isRight = admin.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()));
            if(isRight){
                return admin;
            }else{
                return null;
            }
        }else {
            return null;
        }
    }
    /**
     * 系统初始化时创建超级管理员（仅当admin不存在时）
     */
    @PostConstruct
    public void initAdminAccount() {
        Admin existingAdmin = adminMapper.getAdminByAccount("admin");
        if (existingAdmin == null) {
            Admin defaultAdmin = new Admin();
            defaultAdmin.setAccount("admin");
            defaultAdmin.setPassword(DigestUtils.md5DigestAsHex("admin".getBytes())); // 使用MD5加密
            defaultAdmin.setStatus(1); // 状态正常
            adminMapper.insertAdmin(defaultAdmin);
            System.out.println("初始化超级管理员账号成功");
        }
    }

    @Override
    public List<Users> getAllUsers() {
        return adminMapper.getAllUsers();
    }

    @Override
    public List<Admin> getAllAdmins() {
        return adminMapper.getAllAdmins();
    }

    @Override
    public int addAdmin(Admin admin) {
        // 验证账号长度(2-10位)
        if (admin.getAccount().length() < 2 || admin.getAccount().length() > 10) {
            throw new IllegalArgumentException("账号长度必须为2-10位");
        }
        // 检查账号唯一性
        if (adminMapper.getAdminByAccount(admin.getAccount()) != null) {
            throw new IllegalArgumentException("账号已存在");
        }
        // 加密密码
        admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        admin.setStatus(1);
        return adminMapper.insertAdmin(admin);
    }

    @Override
    public int updateAdminStatus(Integer id, Integer status) {
        return adminMapper.updateAdminStatus(id, status);
    }

    @Override
    public void changePassword(Integer id, String newPassword) {
        // 实现修改密码逻辑
        String encryptedPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        adminMapper.updateAdminPassword(id, encryptedPassword);
    }

    @Override
    public boolean isAccountExist(String account, Integer excludeId) {
        Admin admin = adminMapper.getAdminByAccountExcludeId(account, excludeId);
        return admin != null;
    }

    @Override
    public List<Users> searchUsers(String account, String eml, String tel) {
        return adminMapper.searchUsers( account,  eml,  tel) ;
    }

    @Override
    public void batchDelete(Integer id) {
        adminMapper.batchDeleteByid(id);
    }
}
